IF EXISTS (SELECT * FROM sysobjects WHERE name = 'GetRSVDMemoryMBByTotalMemoryMB' AND type IN ('FN','IF','TF'))
     DROP FUNCTION dbo.GetRSVDMemoryMBByTotalMemoryMB
GO

CREATE FUNCTION dbo.GetRSVDMemoryMBByTotalMemoryMB
     ( @TotalMemoryMB int )
RETURNS int
AS
/*
 *
 * GetRSVDMemoryMBByTotalMemoryMB
 *
 * Purpose:      
 *
 * Inputs:       
 *
 * Assumes:      
 *
 * Effects:      
 *
 * Returns:      
 *
 * Calling
 * Application:  
 *
 * Note(s):      
 *
 * CfgCtrl Keys: 
 *
 * Author(s):    NOVA Concepts Ltd.
 *
 * Change Log:   Initial Release, 06/09/2009, 1.00.0000
 *
 */
BEGIN
     DECLARE @ReturnValue int

     IF ( @TotalMemoryMB IS NULL ) 
     BEGIN
          SET @ReturnValue = NULL 
     END
     ELSE
     BEGIN
          DECLARE @Ranges TABLE ( LowMB int NOT NULL, HighMB int NOT NULL, RSVDMB int NOT NULL )

          INSERT @Ranges ( LowMB, HighMB, RSVDMB ) VALUES (     0,        768,  768 ) -- 0MB-768MB ALL Memory RSVD
          INSERT @Ranges ( LowMB, HighMB, RSVDMB ) VALUES (   769,       2048,  512 ) -- 769MB-2GB
          INSERT @Ranges ( LowMB, HighMB, RSVDMB ) VALUES (  2049,       3584,  768 ) -- 2GB-3.5GB
          INSERT @Ranges ( LowMB, HighMB, RSVDMB ) VALUES (  3585,       5120, 1024 ) -- 3.5GB-5GB
          INSERT @Ranges ( LowMB, HighMB, RSVDMB ) VALUES (  5121,       6656, 1280 ) -- 5GB-6.5GB
          INSERT @Ranges ( LowMB, HighMB, RSVDMB ) VALUES (  6657,       8192, 1536 ) -- 6.5GB-8GB
          INSERT @Ranges ( LowMB, HighMB, RSVDMB ) VALUES (  8193,      10240, 1792 ) -- 8GB-10GB
          INSERT @Ranges ( LowMB, HighMB, RSVDMB ) VALUES ( 10241,      65536, 2048 ) -- 10GB-64GB
          INSERT @Ranges ( LowMB, HighMB, RSVDMB ) VALUES ( 65537, 2147483647, 3072 ) -- 64GB-Above

          SELECT @ReturnValue = RSVDMB 
          FROM   @Ranges
          WHERE  @TotalMemoryMB BETWEEN LowMB AND HighMB
     END

     RETURN @ReturnValue
END
GO
